package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
*@author ${xinpl}
*@Description 权限控制类
*@Date 2020/4/16 11:25
**/
@RestController
@RequestMapping("/Permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    /**
    *@author ${xinpl}
    *@Description 权限数据分页展示
    *@Date 2020/4/16 11:26
    *@Param [queryPageBean]
    *@return com.itheima.constant.Result
    **/
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        System.out.println("/findPage -- "+queryPageBean);
        try {
            PageResult pageResult = permissionService.findPage(queryPageBean);
            return new Result(true,"成功",pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"权限分页失败!");
        }
    }

    /**
    *@author ${xinpl}
    *@Description 新增权限
    *@Date 2020/4/16 11:27
    *@Param [permission]
    *@return com.itheima.constant.Result
    **/
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission){
        System.out.println("/add -- "+permission);
        //先校验
        try {
            if(permissionService.checkPermissionExist(permission.getName())){
                return new Result(false,"权限名已经存在.");
            }else if(permissionService.checkPermissionExist(permission.getKeyword())){
                return new Result(false,"权限编号已经存在.");
            }
            permissionService.add(permission);
            return new Result(true,"新增权限成功.");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新增权限失败!");
        }

    }

    /**
    *@author ${xinpl}
    *@Description 删除权限
    *@Date 2020/4/16 11:27
    *@Param [id]
    *@return com.itheima.constant.Result
    **/
    @RequestMapping("/delete")
    public Result delete(int id){
        System.out.println("/delete -- "+id);
        try {
            permissionService.delete(id);
            return new Result(true,"成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"此权限与用户关联,不能删除!!");
        }
    }

    /**
    *@author ${xinpl}
    *@Description 修改权限
    *@Date 2020/4/16 11:27
    *@Param [permission]
    *@return com.itheima.constant.Result
    **/
    @RequestMapping("/update")
    public Result update(@RequestBody Permission permission){
        System.out.println("/update -- "+permission);
        try {
            if(permissionService.checkPermissionExist(permission.getName())){
                return new Result(false,"权限名已经存在.");
            }else if(permissionService.checkPermissionExist(permission.getKeyword())){
                return new Result(false,"权限编号已经存在.");
            }
            permissionService.update(permission);
            return new Result(true,"成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改权限失败!");
        }
    }

    /**
    *@author ${xinpl}
    *@Description 根据权限id,回显数据
    *@Date 2020/4/16 11:28
    *@Param [id]
    *@return com.itheima.constant.Result
    **/
    @RequestMapping("/findById")
    public Result findById(int id){
        System.out.println("/findById -- "+id);
        try {
            Permission permission = permissionService.findById(id);
            return new Result(true,"数据加载成功",permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"权限回显数据失败!");
        }
    }


}
